//
//  CollectListCell.m
//  GC_OC
//
//  Created by CoderDCLiu on 2023/3/16.
//

#import "CollectListCell.h"
#import "MediaModel.h"

@interface CollectListCell ()

@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, strong) NSMutableArray *tagLabels;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UILabel *infoLabel;

@end

@implementation CollectListCell

- (void)setupSubviews
{
    [super setupSubviews];
    
//    self.contentView.backgroundColor = [UIColor colorWithRed:237.0/255.0 green:239.0/255.0 blue:242.0/255.0 alpha:1];
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_播放_32_defaults 1"]];
    [self.contentView addSubview:iconView];
    self.iconView = iconView;
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(15) textColor:fkColor333333];
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UIImageView *timeIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_定时_32_default_white"]];
    [self.contentView addSubview:timeIcon];
    
    UILabel *timeLabel = [UILabel fk_labelWithFont:fk_adjustFont(13) textColor:fkColor999999];
    [self.contentView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    // 默认创建10个标签
    NSMutableArray *tagLabels = [NSMutableArray array];
    for (NSInteger index = 0; index < 10; index++) {
        UILabel *tagLabel = [UILabel fk_labelWithFont:fk_adjustFont(10) textColor:[UIColor fk_colorWithHex:0x9f9f9f] textAlignment:NSTextAlignmentCenter];
        [self.contentView addSubview:tagLabel];
        [tagLabels addObject:tagLabel];
//        tagLabel.backgroundColor = [UIColor fk_colorWithHex:0x32c5ff alpha:0.1];
        tagLabel.backgroundColor = fkColor(242, 242, 242);
        [tagLabel fk_viewCornerRadius:2 borderWidth:0 borderColor:nil];
    }
    self.tagLabels = tagLabels;
    
    UILabel *infoLabel = [UILabel fk_labelWithFont:fk_adjustFont(8) textColor:fkColor999999];
    [self.contentView addSubview:infoLabel];
    self.infoLabel = infoLabel;
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.centerY.offset(0);
        make.width.height.offset(32);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12);
        make.left.mas_equalTo(fk_adjustW(30));
        make.right.mas_equalTo(fk_adjustW(-80));
    }];
    [timeIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(timeLabel.mas_left).offset(-4);
        make.centerY.equalTo(timeLabel);
    }];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.centerY.equalTo(titleLabel);
    }];
    [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(timeLabel);
        make.bottom.offset(-12);
    }];
}

- (void)setData:(id)data
{
    [super setData:data];
    
    if ([data isKindOfClass:[MediaModel class]]) {
        MediaModel *model = data;
        
        self.titleLabel.text = model.name;
        self.titleLabel.font = model.titleFont;
        if (model.audio_num > 0) {
            self.timeLabel.text = model.audioDuration;
        } else {
            self.timeLabel.text = model.videoDuration;
        }
        
        self.iconView.hidden = !model.isSelected;
        self.contentView.backgroundColor = model.isSelected ? [UIColor fk_colorWithHex:0x32c5ff alpha: 0.1] : [UIColor whiteColor];
        
        if (model.tags.count > 0) {
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(12);
                make.left.mas_equalTo(fk_adjustW(30));
                make.right.mas_equalTo(fk_adjustW(-80));
            }];
            [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.offset(-15);
                make.centerY.equalTo(self.titleLabel);
            }];
        } else {
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.offset(0);
                make.left.mas_equalTo(fk_adjustW(30));
                make.right.mas_equalTo(fk_adjustW(-80));
            }];
            [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.offset(12);
                make.right.offset(-15);
            }];
        }

        // 给标签赋值
        for (NSInteger index = 0; index < self.tagLabels.count; index++) {
            UILabel *tagLabel = self.tagLabels[index];
            if (index < model.tags.count) {
                tagLabel.text = model.tags[index];
                tagLabel.hidden = false;
                tagLabel.frame = [model.tagFrames[index] CGRectValue];
            } else {
                tagLabel.hidden = true;
            }
        }

        if (model.media_class_name && model.media_class_pid_name) {
            self.infoLabel.text = fkFORMAT(@"%@-%@", model.media_class_name, model.media_class_pid_name);
        } else if (model.media_class_pid_name) {
            self.infoLabel.text = fkFORMAT(@"%@", model.media_class_pid_name);
        } else if (model.media_class_name) {
            self.infoLabel.text = fkFORMAT(@"%@", model.media_class_name);
        }
        
    }
}

@end
